import Recording from 'node-record-lpcm16'
import VAD from "webrtcvad";    //这个好像无法在electron中使用

const vad = new VAD(VAD.Mode.NORMAL);

//const file = fs.createWriteStream('test.wav', { encoding: 'binary' })
 
let recorder = new Recording({
    audioType: 'wav',
    sampleRate: 16000,
    threshold:0.5,//声音阈值，超过阈值才会开始录音
    silence: '2.0',  // 检测到多长时间的静音就停止录音。单位：秒
});

recorder.start().stream()
  .on('data', (data:any) => {
    vad.processAudio(data, 16000).then((res:any) => {
      switch (res) {
        case VAD.Event.ERROR:
          console.error("VAD Error", res);
          break;
        case VAD.Event.NOISE:
          console.log("VAD Noise detected");
          break;
        case VAD.Event.SILENCE:
          console.log("VAD Silence detected");
          break;
        case VAD.Event.VOICE:
          console.log("VAD Voice detected");
          // Here is where you might start recording
          break;
      }
    });
  })
  .on('error', console.error);